home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / amiga.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  13KB  |  711 lines

  1. /*
  2.  * NOTE: Portions of this code was directly derived from the Amiga C-Kermit
  3.  * and is copyrighted by Jack J. Rouse.  Please read the following Copyright
  4.  * notice.
  5.  *                                Mikel Matthews
  6. /*
  7.  Author: Jack Rouse
  8.  Contributed to Columbia University for inclusion in C-Kermit.
  9.  Copyright (C) 1986, Jack J. Rouse, 106 Rubin Ct. Apt. A-4, Cary NC 27511
  10.  Permission is granted to any individual or institution to use, copy, or
  11.  redistribute this software so long as it is not sold for profit, provided 
  12.  this copyright notice is retained. 
  13.  
  14.  The file status routines assume all file protection modes are real, instead
  15.  of just delete protection on files and write protection on disks.
  16. */
  17. #include <stdio.h>        /* standard I/O stuff */
  18. #include "exec/types.h"
  19. #include "exec/exec.h"
  20. #include "devices/serial.h"
  21. #include "devices/timer.h"
  22. #include "libraries/dos.h"
  23. #include "libraries/dosextens.h"
  24. #include "intuition/intuition.h"
  25. #include "intuition/intuitionbase.h"
  26. #include "amiga.h"
  27. #include "fcntl.h"
  28. #include "global.h"
  29. #include "iface.h"
  30. #include "smtp.h"
  31. #include "dir.h"
  32. extern    errno;
  33. struct Process *MyProc;
  34. struct CommandLineInterface *MyCLI;
  35. struct FileHandle *Fptr;    /* used for input */
  36.  
  37. /* static definitions */
  38.  
  39. static struct MsgPort *serport;        /* message port for serial comm */
  40. static struct IOExtSer *Read_Request;    /* serial input request */
  41. static struct IOExtSer *Write_Request;    /* serial output request */
  42. static serialopen;
  43.  
  44. /* Exec routines */
  45.  
  46. APTR AllocMem();
  47. struct IORequest *CheckIO();
  48. VOID CloseDevice();
  49. LONG DoIO();
  50. struct Task *FindTask();
  51. struct Message *GetMsg();
  52. LONG OpenDevice();
  53. struct Library *OpenLibrary();
  54. VOID ReplyMsg();
  55. VOID SendIO();
  56. LONG Wait();
  57. LONG WaitIO();
  58. struct Message *WaitPort();
  59.  
  60. /* Exec support */
  61. struct MsgPort *CreatePort();
  62. VOID DeletePort();
  63.  
  64. /* AmigaDOS routines */
  65. BPTR Input();
  66. LONG Read();
  67. LONG WaitForChar();
  68.  
  69. struct asy asy[ASY_MAX];
  70.  
  71. extern x, y, maxcol, MINX, MINY, MAXX;
  72. struct MsgPort *mySerPort;
  73. struct MsgPort *myTimerPort;
  74.  
  75. extern char *mailqdir;
  76.  
  77. struct Store_input {
  78.     char *head;
  79.     char store[MAX_STORE];
  80.     char *tail;
  81.     int    amt;
  82. } Store_input[ASY_MAX];
  83.  
  84. /* Interface list; initialized at compile time to contain
  85.  * just the local interface
  86.  */
  87. struct interface *ifaces;
  88.  
  89. char *ttbuf;
  90.  
  91. char Amiga_drive[255];
  92. char    con_buff[255];
  93. /*
  94.  *  sysinit -- Amiga specific initialization
  95.  */
  96. ioinit() 
  97. {
  98.     register struct IOExtSer *iob;
  99.     int    i;
  100.     int speed;
  101.     char *ptr;
  102.     FILE *ffptr;
  103.     int    fd;
  104.     extern char version[];
  105.  
  106.  
  107.     MyProc = (struct Process *)FindTask((char *)NULL);
  108.  
  109.     MyCLI = (struct CommandLineInterface *)BADDR(MyProc->pr_CLI);
  110.  
  111.     /* allocate serial ports and IO blocks */
  112.     (void) Execute("cd >RAM:tmpfilename", NULL, NULL);
  113.  
  114.     if ( ( ffptr = fopen("RAM:tmpfilename", "r") ) == NULL)
  115.     {
  116.         return;
  117.     }
  118.  
  119.     (void) fgets( Amiga_drive, sizeof(Amiga_drive), ffptr);
  120.     (void) fclose(ffptr);
  121.     (void) unlink("RAM:tmpfilename");
  122.     ptr = Amiga_drive;
  123.     while ( *ptr )
  124.     {
  125.         if (*ptr == '\n')
  126.         {
  127.             *ptr = '\0';
  128.         }
  129.         else if (*ptr == ':')
  130.         {
  131.             *++ptr = '\0';
  132.         }
  133.         else
  134.             ptr++;
  135.     }
  136.  
  137. }
  138.  
  139.  
  140. iostop(str, val)
  141. char *str;
  142. int val;
  143. {
  144.     /* close everything */
  145.     /*
  146.     printf("%s, error = %d\n\r", str, val);
  147.     */
  148.  
  149.     if (serialopen)
  150.     {
  151.         CloseDevice(Read_Request);
  152.     }
  153.     if ( mySerPort )
  154.     {
  155.         DeletePort(mySerPort);
  156.     }
  157.  
  158. }
  159. char Receive_store[MAX_STORE];
  160. char Output_store[MAX_STORE];
  161.  
  162. asy_init(dev, arg1,arg2,bufsize)
  163. int dev, bufsize;
  164. char *arg1,*arg2;
  165. {
  166.     register struct asy *ap;
  167.     register struct interface *if_asy;
  168.     extern struct interface *ifaces;
  169.     register struct IOExtSer *iob;
  170.     char *b, *c;
  171.     int i;
  172.     struct Store_input *store;
  173.     iob = Read_Request;
  174.     
  175.     /*
  176.     printf("ASY_INIT: dev = %d\n\r", dev);
  177.     */
  178.     
  179.     ap = &asy[dev];
  180.  
  181.     if ( ap == NULL)
  182.     {
  183.         printf("asy_init: returing -1\n");
  184.         return(-1);
  185.     }
  186.     ap->tty = malloc(strlen(arg2)+1);
  187.     strcpy(ap->tty, arg2);
  188.  
  189.     if (serialopen)
  190.     {
  191.         printf("asy_init: serial line already open\n");
  192.         return(0);    /* ignore if already open */
  193.     }
  194.  
  195.     Read_Request = (struct IOExtSer *)
  196.         AllocMem((long) sizeof(*Read_Request), MEMF_PUBLIC | MEMF_CLEAR);
  197.  
  198.  
  199.     Read_Request->IOSer.io_Message.mn_ReplyPort = CreatePort(0L, 0L);
  200.  
  201.     if (OpenDevice(SERIALNAME, NULL, Read_Request, NULL))
  202.     {
  203.         iostop("Cant open Read device", 5L);
  204.     }
  205.  
  206.     setmem(&Read_Request->io_TermArray, sizeof(struct IOTArray), 0xc0);
  207.     Read_Request->io_SerFlags = SERF_XDISABLED | SERF_EOFMODE;
  208.     Read_Request->io_Baud = 9600L;
  209.     Read_Request->io_ReadLen = 8L;
  210.     Read_Request->io_WriteLen = 8L;
  211.     Read_Request->io_CtlChar = 0x00;
  212.     Read_Request->io_RBufLen = sizeof(Receive_store);
  213.     Read_Request->io_BrkTime = 0L;
  214.     Read_Request->IOSer.io_Command = SDCMD_SETPARAMS;
  215.  
  216.     DoIO(Read_Request);
  217.  
  218.     Read_Request->IOSer.io_Command = CMD_READ;
  219.     Read_Request->IOSer.io_Length = 1L;
  220.     Read_Request->IOSer.io_Data = (APTR)Receive_store;
  221.  
  222.     Write_Request = (struct IOExtSer *)
  223.         AllocMem((long) sizeof(*Write_Request), MEMF_PUBLIC | MEMF_CLEAR);
  224.  
  225.     b = (BYTE *) Read_Request;
  226.     c = (BYTE *) Write_Request;
  227.  
  228.     for ( i = 0; i < sizeof (struct IOExtSer); i++)
  229.     {
  230.         *c++ = *b++;
  231.     }
  232.  
  233.     Write_Request->io_SerFlags = SERF_XDISABLED;
  234.     Write_Request->IOSer.io_Message.mn_ReplyPort = CreatePort(0L, 0L);
  235.     Write_Request->IOSer.io_Command = CMD_WRITE;
  236.     Write_Request->IOSer.io_Length = 1L;
  237.     Write_Request->IOSer.io_Data = (APTR) Output_store;
  238.  
  239.     store = &Store_input[dev];
  240.     store -> head = store -> tail = store -> store;
  241.     store -> amt = 0;
  242.  
  243.     serialopen = TRUE;
  244.  
  245.     return (0L);
  246.  
  247. }
  248.  
  249. asy_stop(interface)     /* ttclos() */
  250. struct interface *interface;
  251. {
  252.     register struct asy *ap;
  253.  
  254.     if (!serialopen)
  255.     {
  256.         return(0);
  257.     }
  258.     CloseDevice(Read_Request);
  259.     serialopen = FALSE;
  260.     return(0);
  261. }
  262.  
  263.  
  264. asy_ioctl(interface, argc, argv)
  265. struct interface *interface;
  266. int argc;
  267. char *argv[];
  268. {
  269.     if ( argc < 1 ) 
  270.     {
  271.         printf("%d\n", asy[interface->dev].speed);
  272.         return 0;
  273.     }
  274.  
  275.     return ( asy_speed( interface->dev, atoi(argv[0])));
  276. }
  277.  
  278. asy_speed(dev, baud)
  279. int dev, baud;
  280. {
  281.     register struct IOExtSer *iob = Read_Request;
  282.     int    speed;
  283.     int e;
  284.  
  285.     /*
  286.     printf("asy_speed, baud = %d\n\r", baud);
  287.     */
  288.     
  289.     if(baud == 0 || dev >= nasy)
  290.     {
  291.         printf("Baud = %d, dev = %d, returning BAD!!\n", baud, dev);
  292.         return(-1);
  293.     }
  294.     asy[dev].speed = baud;
  295.     /*
  296.     printf("serialopen = %d\n", serialopen);
  297.     */
  298.     if (!serialopen ) 
  299.         return(-1);
  300.  
  301.     /* fill in parameters */
  302.  
  303.     iob->io_CtlChar = 0x1113000L;
  304.  
  305.     if (baud >= 0L && baud <= 19200L) 
  306.     {
  307.         iob->io_Baud = baud;
  308.     }
  309.  
  310.     setmem(&iob->io_TermArray, sizeof(struct IOTArray), 0x12);
  311.  
  312.     iob->io_ReadLen = iob->io_WriteLen = 8L;
  313.     iob->io_StopBits = 1L;
  314.     iob->io_SerFlags = SERF_XDISABLED ;
  315.  
  316.     if ( baud > 19200L)
  317.     {
  318.         iob->io_SerFlags |= SERF_RAD_BOOGIE;
  319.     }
  320.     else
  321.     {
  322.         iob->io_SerFlags &= ~SERF_RAD_BOOGIE;
  323.     }
  324.  
  325.     iob->io_SerFlags &= ~(SERF_PARTY_ON | SERF_EOFMODE);
  326.  
  327.     /* set the parameters */
  328.     
  329.     iob->IOSer.io_Command = SDCMD_SETPARAMS;
  330.  
  331.     DoIO(iob);
  332.     return(0);
  333. }
  334.  
  335.  
  336. asy_output(dev, buf, n)
  337. int dev;
  338. char *buf;
  339. int n;
  340. {
  341.     int err;
  342.     register struct IOExtSer *write = Write_Request;
  343. /*    
  344.     printf("asy_output\n");
  345.     printf("asy_output: dev = %d, count = %d\n", dev, n);
  346.     printf("asy_output: serialopen = %d\n", serialopen);
  347. */    
  348.  
  349.     if (!serialopen) 
  350.     {
  351.         printf("asy_output: returning -1\n");
  352.         return(-1);
  353.     }
  354.     write->IOSer.io_Command = CMD_WRITE;
  355.     write->IOSer.io_Data    = (APTR)buf;
  356.     write->IOSer.io_Length  = (long)n;
  357.     if ( ( err = DoIO(write)) != 0)
  358.     {
  359.         printf("asy_output: err = %d\n", err);
  360.     }
  361.     return(n);
  362. }
  363.  
  364. asy_recv(dev,buf,cnt)
  365. int dev;
  366. char *buf;
  367. unsigned cnt;
  368. {
  369.     register struct IOExtSer *read = Read_Request;
  370.     int tot = 0;
  371.     struct Store_input *store;
  372.     char store_it[MAX_STORE];
  373.     int got = 0;
  374.     int tt, e;
  375.     struct asy *ap;
  376.  
  377. #ifdef DEBUG
  378. printf("asy_recv: dev = %d\n", dev);    
  379. #endif
  380.  
  381.     if ( dev > nasy )
  382.     {
  383.         printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
  384.         return(-1);
  385.     }
  386.  
  387.     ap = &asy[dev];
  388.  
  389.     store = &Store_input[dev];        /* point to current */
  390.  
  391. /* printf("asy_recv: want %d, buffer has %d bytes\n", cnt, store->amt);    */
  392.  
  393.     if ( cnt > 0 && store->amt > 0)
  394.     {
  395.         tt = min(cnt , store->amt);
  396.         tt = min(tt,    MAX_STORE);
  397.         got = 0;
  398.         
  399. /*
  400.  printf ( "(if cnt = %d && store->amt = %d) tt = %d\n", cnt, store->amt, tt);
  401. */
  402.  
  403.         while ( tt-- > 0)
  404.         {
  405.             buf[got++] = *store->tail++;
  406.             if ( store->tail >= &store->store[MAX_STORE])
  407.             {
  408.                 store->tail = store->store;    /* wrap around */
  409.             }
  410.             if ( got >= cnt )
  411.                 break;
  412.         }
  413.         store->amt -= got;
  414.         return( got );
  415.     }
  416.     else if ( cnt == 0 )
  417.     {
  418.         return(0);
  419.     }
  420.  
  421.     /* set up the query */
  422.     read->IOSer.io_Command = SDCMD_QUERY;
  423.  
  424.     /* perform read quickly if possible */
  425.     read->IOSer.io_Flags = IOF_QUICK;
  426.     e = DoIO(read);
  427.     if ( e != 0)
  428.     {
  429.         printf("asy_read: read failed. Error = %d\n", e);
  430.         return(-1);
  431.     }
  432.     if (  (tt = read->IOSer.io_Actual) > 0) 
  433.     {
  434.         /* set up the read */
  435.         read->IOSer.io_Command = CMD_READ;
  436.         read->IOSer.io_Data    = (APTR)ap->recv_buf;
  437.         read->IOSer.io_Length  = tt;
  438.  
  439.         e =  DoIO(read);
  440.  
  441.         if ( e != 0)
  442.         {
  443.             printf("asy_read: read failed. Error = %d\n", e);
  444.             return(-1);
  445.         }
  446.         tt = read->IOSer.io_Actual;
  447.         got = 0;
  448.         while (tt-- > 0)
  449.         {
  450.             *store->head++ = ap->recv_buf[got++];
  451.             if ( store->head == &store->store[MAX_STORE] )
  452.             {
  453.                 /* printf("xx got wrap\n"); */
  454.                 store->head = store->store;
  455.             }
  456.             store->amt++;
  457.         }
  458.         if ( cnt > 0 && store->amt > 0)
  459.         {
  460.             tt = min(cnt , store->amt);
  461.             tt = min(tt,    MAX_STORE);
  462.             got = 0;
  463.         
  464. /*    printf ( "(if cnt = %d && store->amt = %d) tt = %d\n", cnt, store->amt, tt); */
  465.  
  466.             while ( tt-- > 0)
  467.             {
  468.                 buf[got++] = *store->tail++;
  469.                 if ( store->tail >= &store->store[MAX_STORE])
  470.                 {
  471.                     store->tail = store->store;    /* wrap around */
  472.                 }
  473.                 if ( got >= cnt )
  474.                     break;
  475.             }
  476.             store->amt -= got;
  477.             return( got );
  478.         }
  479.         return ( 0 );
  480.     }
  481.     return (got);
  482.  
  483. }
  484.  
  485. long
  486. clksec()
  487. {
  488.     long tloc;
  489.  
  490.     time(&tloc);
  491.     return(tloc);
  492.  
  493. }
  494.  
  495. /* Create a directory listing in a temp file and return the resulting file
  496.  * descriptor. If full == 1, give a full listing; else return just a list
  497.  * of names.
  498.  */
  499. FILE *
  500. dir(path,full)
  501. char *path;
  502. int full;
  503. {
  504.     FILE *fp;
  505.     char cmd[50];
  506.  
  507.     if(full)
  508.         sprintf(cmd,"list >RAM:dir.tmp %s",path);
  509.     else
  510.         sprintf(cmd,"dir >RAM:dir.tmp %s",path);
  511.     system(cmd);
  512.     fp = fopen("RAM:dir.tmp","r");
  513.     unlink("RAM:dir.tmp");
  514.     return fp;
  515. }
  516.  
  517. disable()
  518. {
  519.     /* Disable(); */
  520. }
  521.  
  522. restore()
  523. {
  524.     /* Enable(); */
  525. }
  526.  
  527. stxrdy()
  528. {
  529.     return(1);
  530. }
  531.  
  532. Fail(str)
  533. char *str;
  534. {
  535.     char mybuff[255];
  536.     printf("Failure: %s\n", str);
  537. }
  538. eihalt()
  539. {
  540.     FILE *fp;
  541.     return(0);
  542. }
  543. memstat()
  544. {}
  545. int KeepGoing = TRUE;
  546. char
  547. kbread()
  548. {
  549.     char c;
  550.     if ( WaitForChar(Output(), 2) == 0)
  551.     {
  552.         return(-1);
  553.     }
  554.     Read( Input(), &c, 1);
  555.     return(c);
  556. }
  557.  
  558. /* wildcard filename lookup */
  559. static    DIR     *dirp;
  560. static    struct FileInfoBlock *dp;
  561. static     IsOpen = 0;
  562. filedir(name, times, ret_str)
  563. char *name;
  564. int times;
  565. char *ret_str;
  566. {
  567.     char *ptr;
  568.     char *index();
  569.     char *rindex();
  570.     char *name_ptr;
  571.     /*
  572.      * Make sure that the NULL is there incase we don't find anything
  573.      */
  574.     ret_str[0] = NULL;
  575.     if ( ( name_ptr = rindex ( name, '.')) != NULL)
  576.     {
  577.         name_ptr++;
  578.     }
  579.     else
  580.     {
  581.         /*
  582.          * no name to find, so go back
  583.          */
  584.         return;
  585.     }
  586.  
  587.  
  588.     if ( times == 0 && IsOpen == 0 )
  589.     {
  590.         if ( (dirp = opendir(mailqdir)) == NULL ) {
  591.             return;
  592.         }
  593.         IsOpen = 1;
  594.     }
  595.  
  596.     for ( dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
  597.         if ( ( ptr = index( dp ->fib_FileName, '.')) == NULL) {
  598.             continue;
  599.         }
  600.         else {
  601.             ptr++;
  602.             if ( strncmp(ptr, name_ptr, strlen(name_ptr)) != 0) {
  603.                 continue;
  604.             }
  605.             else {
  606.                 sprintf(ret_str,"%s", dp->fib_FileName);
  607.                 break;
  608.             }
  609.         }
  610.     }
  611.     if ( dp == NULL )
  612.     {
  613.         IsOpen = 0;
  614.         closedir(dirp);
  615.     }
  616. }
  617.  
  618.  
  619. /* checks the time then ticks and updates ISS */
  620. static long clkval = 0;
  621. void
  622. check_time()
  623. {
  624.     int32 iss();
  625.  
  626.     if(clkval != clksec()){
  627.         clkval = clksec();
  628.         tick();
  629.         (void)iss();
  630.     }
  631. }
  632.  
  633. memcmp(str1, str2, len)
  634. char *str1, *str2;
  635. int len;
  636. {
  637.     return(strncmp(str1, str2, len));
  638. }
  639.  
  640. memset(str, value, len)
  641. char *str;
  642. int value, len;
  643. {
  644.     while ( len-- !=  0)
  645.     {
  646.         *str++ = value;
  647.     }
  648. }
  649.  
  650. memcpy(str1, str2, len)
  651. char *str1, *str2;
  652. int len;
  653. {
  654.     while ( len-- != 0 )
  655.     {
  656.         *str1++ = *str2++;
  657.     }
  658. }
  659.  
  660. /* Change working directory */
  661. docd(argc,argv)
  662. int argc;
  663. char *argv[];
  664. {
  665.     char dirname[128],*getwd();
  666.  
  667.     if(argc > 1){
  668.         if(chdir(argv[1]) == -1){
  669.             printf("Can't change directory\n");
  670.             return 1;
  671.         }
  672.     }
  673.     if(getwd(dirname,0) != NULLCHAR){
  674.         printf("%s\n",dirname);
  675.     }
  676.     return 0;
  677. }
  678.  
  679. /* List directory to console */
  680. dodir(argc,argv)
  681. int argc;
  682. char *argv[];
  683. {
  684.     char *path;
  685.     char buff[255];
  686.  
  687.     if ( argc > 2)
  688.         sprintf(buff, "dir %s\n", argv[1]);
  689.     else
  690.         sprintf(buff, "dir");
  691.  
  692.     Execute(buff, NULL, NULL);
  693.     return 0;
  694. }
  695.  
  696. doshell()
  697. {
  698.     if ( ( Fptr = Open("CON:0/0/640/40/CLI", MODE_NEWFILE) ) == 0L)
  699.     {
  700.         printf("doshell: could not open window.  Error = %d\n", IoErr());
  701.         return;
  702.     }
  703.     if ( Execute("", Fptr, NULL) != -1)
  704.     {
  705.         printf("Could not execute the newcli command.\n");
  706.     }
  707.     (void)Close(Fptr);
  708.  
  709.     return;
  710. }
  711.